$(function(){
    var datas=[];
	ym.ajax({
		url: "/userChild/getUserChild",
		success: function (data) {
		   	if(data.res == 1){
		   		$('.baobao').html("");
               if(data.obj.length > 0){
					var html ="";
					datas=data.obj;
					sessionStorage.setItem('datas',JSON.stringify(datas));
					for(var i=0;i<data.obj.length;i++){
						if(i == 0){
							html += '<option value="'+data.obj[i].childId+'" selected>'+data.obj[i].childRealname+'</option>';
						}else{
							html += '<option value="'+data.obj[i].childId+'">'+data.obj[i].childRealname+'</option>';
						}
		   			}
                    $('.baobao').append(html);
                    var childId = $(".baobao option:selected").val();
			        console.log(childId);
			        ones(childId);
		   		}else{
		   			window.location.href = "vaccine1.html";
		   			console.log("没有宝宝");
				}
		   	}else{console.log("获取宝宝失败!")}
		}
	});
        
   	$('.baobao').on('change',function(){
		var childIds = $(this).val();
		ones(childIds);
	})
	
	/*接种记录*/
	function ones(childId){
		$('.jilus').html("");
		ym.ajax({
			url:'/vaccine/queryVaccineHistoryPlanAll',
			param:{ 
				childrenId:childId,
			},
			success:function(data){
				console.log(data);
				if(data.res ==1){
					if(data.obj.length > 0){
						for(var i=0;i<data.obj.length;i++){
							var html = "";
							var arr =[];
			   				var arr2 = [];
			   				var arr3= [];
							var zhi = data.obj[0].vaccineTypeName.indexOf(',');
				   			if(zhi == -1){
				   				data.obj[i].vaccineTypeName  = data.obj[i].vaccineTypeName+",";
				   			}
				   			arr = data.obj[i].vaccineTypeName.split(",");
				   			arr3 = data.obj[i].vaccineTypeName.split("/");
				   			html += '<ul class="lists">';
				   			html += '<h3 class="am-padding-sm backwhite">'+arr3[3]+'</h3>';
				   			html += '<hr class="am-margin-0">';
				   			for(var j=0;j<arr.length;j++){
				   				arr2 = arr[j].split('/');
				   				if(arr2[1] != undefined){
				   					html += '<li class="color46c8ff list list backwhite">';
					   				html += '<div class="displayfx package">';
					   				html += '<p class="w10 padding">';
					   				html += '<span class="yuandian"></span>';
					   				html += '</p>';
					   				html += '<p class="w80" style="line-height: 50px;">';
					   				html += '<span class="color333">'+arr2[1]+'<i class="colorb6bcce font15 am-padding-left-xs">(第'+arr2[0]+'剂/共'+data.obj[i].totalAgent+'剂)</i></span>'
					   				if(data.obj[i].vaccinationTime == null){
					   					html += '<span class="colorb6bcce font14"></span>';
					   				}else{
					   					html += '<span class="colorb6bcce font14">'+data.obj[i].vaccinationTime+'</span>';
					   				}
					   				html += '</div>';
					   				html += '</li>';
				   				}
				   				
				   			}
				   			html += '</ul>';
				   			$('.jilus').append(html);
						}
					}else{
						var html = "";
						html += '<ul class="lists am-text-center backwhite am-padding-sm" style="border-top:1px solid gainsboro;"><img style="height:150px" src="../img/no.jpg"></ul>';
						$('.jilus').append(html);
					}
					
					
				}else{console.log("数据获取失败！")}
			}
		});
	}
	/*跳转到添加接种记录页面*/
	$('#addBtn button').click(function(){
		var curBB=$('.baobao').find("option:selected").val();
		var datas=JSON.parse(sessionStorage.getItem('datas'));
		for (var i = 0; i < datas.length; i++) {
			if(datas[i].childId==curBB){
				sessionStorage.setItem('curChild',JSON.stringify(datas[i]));
			}
		}
		window.location.href='addVaccine.html';
	})
})
